package top.wilbert.collegelink.classification.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import top.wilbert.collegelink.classification.vo.Catalog2Vo;
import top.wilbert.collegelink.common.utils.PageUtils;
import top.wilbert.collegelink.common.utils.Query;

import top.wilbert.collegelink.classification.dao.RegionDao;
import top.wilbert.collegelink.classification.entity.RegionEntity;
import top.wilbert.collegelink.classification.service.RegionService;


@Service("regionService")
public class RegionServiceImpl extends ServiceImpl<RegionDao, RegionEntity> implements RegionService {

    @Autowired
    ThreadPoolExecutor executor;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<RegionEntity> page = this.page(
                new Query<RegionEntity>().getPage(params),
                new QueryWrapper<RegionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public Map<Long, List<Catalog2Vo>> getCatalogJson() {
        System.out.println("查询了数据库。。。");
        List<RegionEntity> selectList = baseMapper.selectList(null);
        List<RegionEntity> level1Catalog = getRegionListByPid(selectList, 0L);
        //2、封装数据
        Map<Long, List<Catalog2Vo>> catalogJson = level1Catalog.stream().collect(Collectors.toMap(k -> k.getRegionId(), l1 -> {
            //1、每一个的一级分类，查到这个一级分类的二级分类
            List<RegionEntity> level2Catalog = getRegionListByPid(selectList, l1.getRegionId());
            //2、封装上面的结果
            List<Catalog2Vo> Catalog2Vos = null;
            if (level2Catalog != null) {
                Catalog2Vos = level2Catalog.stream().map(l2 -> {
                    Catalog2Vo catalog2Vo = new Catalog2Vo(l2.getPid(), null, l2.getRegionId(), l2.getName());
                    //1、找当前2级分类的三级分类封装成vo
                    List<RegionEntity> level3Catalog = getRegionListByPid(selectList, l2.getRegionId());
                    if (level3Catalog != null) {
                        List<Catalog2Vo.Catalog3Vo> collect = level3Catalog.stream().map(l3 -> {
                            //2、封装成指定格式
                            Catalog2Vo.Catalog3Vo catalog3Vo = new Catalog2Vo.Catalog3Vo(l3.getPid(), l3.getRegionId(), l3.getName());
                            return catalog3Vo;
                        }).collect(Collectors.toList());
                        catalog2Vo.setCatalog3List(collect);
                    }
                    return catalog2Vo;
                }).collect(Collectors.toList());
            }
            return Catalog2Vos;
        }));

        return catalogJson;
    }

    private List<RegionEntity> getRegionListByPid(List<RegionEntity> selectList, Long pid) {
        List<RegionEntity> collect = selectList.stream().filter(item -> item.getPid().equals(pid)).collect(Collectors.toList());
        return collect;
    }

    @Override
    public List<RegionEntity> listWithTree() {
        //1、查出所有分类
        List<RegionEntity> entities = baseMapper.selectList(null);

        //2、组装成父子的树形结构
        //找到所有的一级分类
        List<RegionEntity> level1Menus = entities.stream().filter((regionEntity) -> {
            return regionEntity.getPid().equals(0L);
        }).map((menu)->{
            menu.setChildren(getChildren(menu,entities));
            return menu;
        }).collect(Collectors.toList());

        return level1Menus;
    }

    //递归查出所有菜单的子菜单
    private List<RegionEntity> getChildren(RegionEntity root,List<RegionEntity> all){
        List<RegionEntity> children = all.stream().filter(regionEntity -> {
            return regionEntity.getPid().equals(root.getRegionId());
        }).map(regionEntity -> {
            //找到子菜单
            regionEntity.setChildren(getChildren(regionEntity,all));
            return regionEntity;
        }).collect(Collectors.toList());

        return children;
    }
}
